Skip to content

Implement XDR decoder classes for RFC 4506 compliant binary decoding#56

Merged
streamich merged 3 commits intomasterfrom
copilot/fix-55
Sep 16, 2025
Merged

Implement XDR decoder classes for RFC 4506 compliant binary decoding#56
streamich merged 3 commits intomasterfrom
copilot/fix-55

Conversation

Copy link
Contributor

Copilot AI commented Sep 16, 2025

This PR implements two XDR (External Data Representation Standard) decoder classes to complement the existing XDR encoder functionality, providing complete RFC 4506 compliant binary data processing capabilities.

Implementation

XdrDecoder - Lower-level decoder

A foundational decoder class that uses the Reader interface for explicit type-based XDR decoding:

import { XdrDecoder } from '@jsonjoy.com/json-pack/xdr';

const decoder = new XdrDecoder();
const data = new Uint8Array([0, 0, 0, 42, 0, 0, 0, 5, 104, 101, 108, 108, 111, 0, 0, 0]);

decoder.reader.reset(data);
const value = decoder.readInt();        // 42
const length = decoder.readInt();       // 5
const text = decoder.readString();      // "hello"

XdrSchemaDecoder - Higher-level schema-aware decoder

A schema-driven decoder that automatically handles complex XDR structures based on type definitions:

import { XdrSchemaDecoder, XdrUnion } from '@jsonjoy.com/json-pack/xdr';

const schema = {
  type: 'struct',
  fields: [
    [{ type: 'int' }, 'id'],
    [{ type: 'string' }, 'name'],
    [{ 
      type: 'union',
      arms: [[0, { type: 'string' }], [1, { type: 'int' }]]
    }, 'contact']
  ]
};

const decoder = new XdrSchemaDecoder();
const result = decoder.decode(binaryData, schema);
// Returns: { id: 123, name: "Alice", contact: XdrUnion { discriminant: 0, value: "alice@example.com" } }

Key Features

  • RFC 4506 Compliance: Proper 4-byte alignment, big-endian byte order, and padding handling
  • Complete Type Support: All XDR primitives (int, unsigned_int, hyper, float, double, boolean, etc.)
  • Schema Integration: Full compatibility with existing XDR schema types from types.ts
  • Complex Structures: Support for structs, unions, arrays, enums, and nested compositions
  • Round-trip Compatibility: Seamless integration with existing XdrEncoder and XdrSchemaEncoder
  • Size Validation: Automatic constraint checking for variable-length data
  • Error Handling: Comprehensive validation and meaningful error messages

Testing

Added 65 comprehensive tests covering:

  • All primitive type decoding with round-trip verification
  • Complex nested structures and edge cases
  • Size constraint validation and error conditions
  • Integration with existing encoder classes
  • Special values (NaN, Infinity, boundary conditions)

All existing tests continue to pass, ensuring backward compatibility.

Fixes #55.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
@streamich streamich marked this pull request as ready for review September 16, 2025 18:18
Copilot AI changed the title [WIP] Implement XDR decoder Implement XDR decoder classes for RFC 4506 compliant binary decoding Sep 16, 2025
Copilot AI requested a review from streamich September 16, 2025 18:20
@streamich streamich merged commit e8a68dc into master Sep 16, 2025
4 checks passed
@streamich streamich deleted the copilot/fix-55 branch September 16, 2025 18:27
@github-actions
Copy link

🎉 This PR is included in version 1.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement XDR decoder

2 participants